package cn.edu.scut.hsrc.trie;

import java.util.ArrayList;
import java.util.List;

import cn.edu.scut.hsrc.bean.Word;

/**
 * 树节点类
 * @file Node.java
 * @author feiyu
 *
 */

public class Node {
	
	/**
	 * 父亲节点
	 */
	private Node parent;
	
	/**
	 * 儿子节点
	 */
	private List<Node> children;
	/**
	 * 这个节点所包含的词的集合
	 */
	private List<Word>	words;
	
	/**
	 * 包含的内部文档编号，注意这里的文档和聚类的输入文档是两码事
	 */
	private List<Integer> internalDocs;
	
	/**
	 * 如果有文档以该节点结尾，那这个字段就有意义，它包含了以该节点结尾的文档编号
	 */
	private List<Integer> endDocs;
	
	
	/**
	 * 
	 * 构造函数
	 */
	public Node() {
		// TODO Auto-generated constructor stub
		this.parent = null;
		this.children = new ArrayList<Node>();
		this.words = new ArrayList<Word>();
		this.internalDocs = new ArrayList<Integer>();
		this.endDocs = new ArrayList<Integer>();
	}
	
	
	/**
	 * 获取儿子节点的数目
	 * @return
	 */
	public int getChildNum() {
		return this.children.size();
	}
	
	/**
	 * 获取index对应的儿子节点
	 * @param index
	 * @return
	 */
	public Node getChild(int index) {
		return this.children.get(index);
	}
	
	/**
	 * 获取该节点包含的词集合中的第index个词
	 * @param index
	 * @return
	 */
	public Word getWord(int index) {
		return this.words.get(index);
	}
	
	/**
	 * 获取父亲节点
	 * @return
	 */
	public Node getParent() {
		return this.parent;
	}
	
	/**
	 * 设置父亲节点
	 * @param parent
	 */
	public void setParent(Node parent) {
		this.parent = parent;
	}
	
	/**
	 * 获取词的个数
	 * @return
	 */
	public int getWordCount() {
		return this.words.size();
	}
	
	/**
	 * 添加内部文档编号
	 * @param index
	 */
	public void addInternalDoc(int index) {
		this.internalDocs.add(index);
	}
	
	/**
	 * 添加结尾文档编号
	 * @param index
	 */
	public void addEndDoc(int index) {
		this.endDocs.add(index);
	}
	
	/**
	 * 获取内部文档编号集合
	 * @return
	 */
	public List<Integer> getInternalDocs() {
		return this.internalDocs;
	}

	/**
	 * 设置内部文档编号集合
	 * @param internalDocs
	 */
	public void setInternalDocs(List<Integer> internalDocs) {
		for (int integer : internalDocs) {
			this.internalDocs.add(integer);
		}
	}
	
	/**
	 * 添加内部文档编号集合
	 * @param internalDocs
	 */
	public void addInternalDocs(List<Integer> internalDocs) {
		for (int integer : internalDocs) {
			this.internalDocs.add(integer);
		}
	}
	/**
	 * 获取该节点包含的词集合
	 * @return
	 */
	public List<Word> getWords() {
		return this.words;
	}

	/**
	 * 设置该节点包含的词集合
	 * @param words
	 */
	public void setWords(List<Word> words) {
		this.words = words;
	}
	
	/**
	 * 添加词
	 * @param word
	 */
	public void addWord(Word word) {
		this.words.add(word);
	}
	
	/**
	 * 移除儿子节点
	 * @param index
	 */
	public void removeChild(int index) {
		this.children.remove(index);
	}
	
	/**
	 * 添加儿子节点
	 * @param child
	 */
	public void addChild(Node child) {
		this.children.add(child);
	}


	@Override
	public String toString() {
		// TODO Auto-generated method stub
		String text = "internalDocs: "+internalDocs.toString()+"\n";
		text += "endDocs: "+this.endDocs.toString()+"\n";
		text += "words: ";
		for (Word word : this.words) {
			text += word+",";
		}
		text += "\n";
		text += "childrenSize: "+this.children.size()+"\n\n";
		return text;
	}
	/**
	 * 获取结束文档集合
	 * @return
	 */
	public List<Integer> getEndDocs() {
		return this.endDocs;
	}

	/**
	 * 设置结束文档集合
	 * @param endDocs
	 */
	public void setEndDocs(List<Integer> endDocs) {
		this.endDocs = endDocs;
	}
	
	public String wordsToString() {
		/*String text = "";
		for (Word word : this.words) {
			text += word.getText()+",";
		}
		text = text.trim();
		if(!text.equals(""))
			text = text.substring(0, text.length()-1);
		return text;*/
		
		String text = "";
		for (Word word : this.words) {
			text += word.getText();
		}
		return text;
	}
}

